import 'package:flutter/material.dart';

import 'package:dio/dio.dart';

class MemberPage extends StatefulWidget {
  _MemberPageState createState() => _MemberPageState();
}

class _MemberPageState extends State<MemberPage> {

  TextEditingController typeController =TextEditingController();
  String showText = "欢迎您来带美好人间";

  Future getHttp(String typeText)async{
    try {
      Response resp;
      var data = {"name":typeText};
      String url = "https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian";
      resp = await Dio().get(url,queryParameters: data);
      return resp.data;
    } catch (e) {
      return print(e);
    }
  }

  void _choiceAction(){
    print("开始选择你喜欢的类型");
    if (typeController.text.toString() == ""){
      showDialog(
        context: context,
        builder: (context)=>AlertDialog(title: Text("类型不能为空"))
      );
      return;
    }
    getHttp(typeController.text.toString()).then((value){
      setState(() {
       showText=value["data"]["name"].toString(); 
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
       child: Scaffold(
         appBar: AppBar(
           title: Text("美好人间"),
         ),
         body: Container(
           child: Column(
             children: <Widget>[
               TextField( // 输入框
                 controller: typeController,
                 decoration: InputDecoration(
                   contentPadding: EdgeInsets.all(10),
                   labelText: "美女类型",
                   helperText: "请输入你喜欢的类型"
                 ),
                 autofocus: false,  // 关闭自动对焦
               ),
               RaisedButton(
                 onPressed: _choiceAction,
                 child: Text("选择完毕"),
               ),
               Text(
                 showText,
                 overflow: TextOverflow.ellipsis,
                 maxLines: 1,
               )
             ],
          
           ),
         ),
       )
    );
  }


}

